package top.lidee.module.backendApi.twice.management.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.AccountInfo;
import top.lidee.common.service.twice.applet.MtStoreCateService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.PageUtils;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.pojo.applet.MtStoreCateEntity;
import top.lidee.repository.model.twice.vo.management.MtStoreCateSaveOrUpdateVo;
import top.lidee.repository.validator.groups.Update;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author 张一恒
 * @version 1.0
 * @description:  行业分类（商户店铺分类）管理
 * @date 2024/10/8 11:21
 */
@Api("行业分类（商户店铺分类）管理")
@RestController
@RequestMapping("/backendApi/industryCateManage")
public class IndustryCateManageController {

    @Autowired
    private MtStoreCateService mtStoreCateService;

    /**
     * 查询所有行业分类 一级分类
     */
    @RequestMapping(value = "/listLeve1",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有行业分类 一级分类")
    public ResponseResult listLeve1(HttpServletRequest request){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.listLeve1(accountInfo);
    }


    /**
     * 查询所有行业分类 树形结构
     */
    @RequestMapping(value = "/listTree",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有行业分类 树形结构")
    public ResponseResult listTree(HttpServletRequest request){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.listTree(accountInfo);
    }


    /**
     * 新增/保存分类
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "新增/保存分类")
    public ResponseResult save(HttpServletRequest request, @RequestBody @Valid MtStoreCateSaveOrUpdateVo vo){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.savedIn(accountInfo,vo);
    }


    /**
     * 根据id查询
     */
    @RequestMapping(value = "/info/{cateId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询")
    public ResponseResult info(HttpServletRequest request, @PathVariable("cateId") Integer cateId){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.info(accountInfo,cateId);
    }


    /**
     * 根据id修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "根据id修改")
    public ResponseResult update(HttpServletRequest request, @RequestBody @Validated(value = Update.class) MtStoreCateSaveOrUpdateVo vo){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.update(accountInfo,vo);
    }


    /**
     * 单个或批量删除
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(value = "单个或批量删除")
    public ResponseResult update(HttpServletRequest request, @RequestBody Integer[] cateIds){
        String token = request.getHeader("Access-Token");
        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        return mtStoreCateService.delete(accountInfo,cateIds);
    }


}
